import { PageAlias } from './route.enum';
import { ConfigCenterSingleton } from '../config-center/config-center.singleton';
import { PagePathMapping } from './page-path.mapping';
import { RouteBuilder } from './router-builder';

function getPathConfigByPageAlias(
    pageAlias: PageAlias,
    pathIndexIfIsArray: number = 0,
): {
    host: string;
    path: string;
} {
    const path = PagePathMapping?.[pageAlias];

    if (!path) {
        return;
    }

    const siteConfig = ConfigCenterSingleton.siteConfig;

    return {
        host: siteConfig?.domainConfigs?.find((domain) => domain.domain === path.domain)?.host,
        path: path.path instanceof Array ? path.path[pathIndexIfIsArray] : path.path,
    };
}

function getPageUrl(
    pageAlias: PageAlias,
    params?: { [key: string]: string | boolean | number },
    isHttps?: boolean,
) {
    const pageConfig = getPathConfigByPageAlias(pageAlias);

    if (!pageConfig) {
        return '';
    }

    const href = new RouteBuilder(pageConfig?.host)
        .setProtocol(isHttps ? 'https' : 'http')
        .setPath(pageConfig?.path)
        .build(params);

    return href;
}

export { getPathConfigByPageAlias, getPageUrl };
